package org.jeecg.modules.wo.wobasepeople.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wo.nucleicmessage.entity.WoNucleicMessage;
import org.jeecg.modules.wo.nucleicmessage.service.IWoNucleicMessageService;
import org.jeecg.modules.wo.people.entity.WoPeople;
import org.jeecg.modules.wo.people.service.IWoPeopleService;
import org.jeecg.modules.wo.persontype.service.IWoPersonTypeService;
import org.jeecg.modules.wo.utils.KyqApiUtils;
import org.jeecg.modules.wo.wobasepeople.entity.WoBasePeople;
import org.jeecg.modules.wo.wobasepeople.mapper.WoBasePeopleMapper;
import org.jeecg.modules.wo.wobasepeople.service.IWoBasePeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: wo_base_people
 * @Author: jeecg-boot
 * @Date:   2022-03-29
 * @Version: V1.0
 */
@Service
@Slf4j
public class WoBasePeopleServiceImpl extends ServiceImpl<WoBasePeopleMapper, WoBasePeople> implements IWoBasePeopleService {
    @Autowired
    IWoNucleicMessageService iWoNucleicMessageService;
    @Autowired
    private IWoPersonTypeService woPersonTypeService;
    @Autowired
    private IWoPeopleService woPeopleService;

    /**
     * 通过基础人员表去获取人员核酸信息 暂时弃用
     * @param typeId
     */
    @Override
    public void getInsp(String typeId) {
        QueryWrapper<WoBasePeople> queryWrapper = new QueryWrapper<>();
        /*if(StringUtils.isNotEmpty(typeId)){
            String ids=woPersonTypeService.queryTreeChildIds(typeId);
            queryWrapper.in("psn_type_id",ids.split(","));
        }*/
        List<WoBasePeople>  peopleList=this.list(queryWrapper);
        for(WoBasePeople basePsn:peopleList){
            if(StringUtils.isBlank(basePsn.getPerNucleiccode())){
                //人员没有抗疫情编码则不执行获取核酸检测记录
                continue;
            }
            ////通过接口获取核酸检测数据
            JSONObject inspObj= KyqApiUtils.getInspByKyqCode(basePsn.getPerNucleiccode());
            if(inspObj!=null&&inspObj.getInteger("status")==200){
                JSONObject dataObj=inspObj.getJSONObject("data");
                this.saveNucleic(basePsn,dataObj.getDate("feedBackTime"),dataObj.getString("checkResult"),dataObj.getString("detectionName"));
                log.info(String.format("定时任务，获取人员 姓名："+basePsn.getPerName()+",核酸检测数据成功！时间:" + DateUtils.getTimestamp()));
            }else{
                log.warn(String.format("定时任务，获取人员 姓名："+basePsn.getPerName()+",核酸检测数据失败！时间:" + DateUtils.getTimestamp()));
            }
        }
    }
    /**
     * 通过基础人员表去获取人员核酸信息 暂时弃用
     * @param typeId
     */
    @Override
    public void getInspByDevice(String typeId) {
        QueryWrapper<WoBasePeople> queryWrapper = new QueryWrapper<>();
       /* if(StringUtils.isNotEmpty(typeId)){//尚未完善
            String ids=woPersonTypeService.queryTreeChildIds(typeId);
            queryWrapper.in("psn_type_id",ids.split(","));
        }*/
        List<WoBasePeople>  peopleList=this.list(queryWrapper);
        for(WoBasePeople basePsn:peopleList) {
            JSONObject inspObj=null;
            if (StringUtils.isNotBlank(basePsn.getPerNucleiccode())) {
                //人员没有抗疫情编码
                /**
                 * 通过抗疫情编码 从抗疫情获取核酸检测记录
                 */
                inspObj= KyqApiUtils.nucleicAcidTest(4,basePsn.getPerNucleiccode(),2,"","");
            }else if(StringUtils.isNotBlank(basePsn.getPerIdcard())){
                /**
                 * 通过人员身份证号码 从抗疫情获取核酸检测记录
                 */
                inspObj= KyqApiUtils.nucleicAcidTest(3,basePsn.getPerIdcard(),2,"","");
            }else{continue;}
            if(inspObj.getInteger("code").equals(0)&&inspObj.getString("msg").equals("success")){
                JSONObject data= (JSONObject) inspObj.get("data");
                if(data!=null){
                    basePsn.setPerNucleiccode(data.getString("personId"));
                    this.saveNucleic(basePsn,data.getDate("confirmTime"),data.getString("checkResult"),"");
                    log.info(String.format("定时任务，获取人员 姓名："+basePsn.getPerName()+",核酸检测数据成功！时间:" + DateUtils.getTimestamp()));
                }else{
                    log.warn(String.format("定时任务，获取人员 姓名："+basePsn.getPerName()+",核酸检测数据失败！时间:" + DateUtils.getTimestamp()));
                }
            }else{
                log.warn(String.format("定时任务，获取人员 姓名："+basePsn.getPerName()+",核酸检测数据失败！时间:" + DateUtils.getTimestamp()));
            }
        }
    }

    private void saveNucleic(WoBasePeople basePsn,Date confirmTime,
                             String checkResult,
                             String detectionName){
        WoNucleicMessage ns=new WoNucleicMessage();
        ns.setPerId(basePsn.getId());
        ns.setNucleicAddress(detectionName);//核酸检测点
        ns.setNucleicDate(confirmTime);//核酸检测时间
        ns.setNucleicResult(checkResult);
        //保存核酸加测记录
        iWoNucleicMessageService.save(ns);
        //将最新核酸检测信息更新至人员表，便于查询
        basePsn.setNucleicAddress(detectionName);
        basePsn.setNucleicDate(confirmTime);
        basePsn.setNucleicResult(checkResult);
        this.updateById(basePsn);
        UpdateWrapper<WoPeople> upWrapper=new UpdateWrapper<>();
        upWrapper.set("nucleic_date",confirmTime);
        upWrapper.set("nucleic_result",checkResult);
        upWrapper.set("nucleic_address",detectionName);
        upWrapper.set("per_nucleiccode",basePsn.getPerNucleiccode());
        upWrapper.eq("base_people_id",basePsn.getId());
        woPeopleService.update(upWrapper);
    }
}
